package org.ibase4j.jwt.security;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import top.ibase4j.core.support.http.SessionUser;

import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author jie
 * @date 2018-11-23
 */
@Getter
@Setter
public class JwtUser implements UserDetails {

    public SessionUser change(){
        return new SessionUser(this.getId(),this.getUsername(),this.getEmail(),false);
    }

//    @JsonIgnore
    private  Long id;

    private  String username;

    @JsonIgnore
    private  String password;

    private  String avatar;

    private  String email;

    @JsonIgnore
    private  Collection<? extends GrantedAuthority> authorities;

    Set<String> roles = new LinkedHashSet<>();
    Set<String> authoritySet = new LinkedHashSet<>();

    private  boolean enabled;

    private Timestamp createTime;

    @JsonIgnore
    private  Date lastPasswordResetDate;

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    public JwtUser() {
    }

    public JwtUser(Long id, String username, String password, String avatar, String email, List<String> authoritySet, boolean enabled, Timestamp createTime, Date lastPasswordResetDate) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.avatar = avatar;
        this.email = email;
        this.roles = new HashSet<>();
        this.authoritySet = new HashSet<>(authoritySet);
        this.authorities = mapToGrantedAuthorities(authoritySet);
        this.enabled = enabled;
        this.createTime = createTime;
        this.lastPasswordResetDate = lastPasswordResetDate;
    }

    @JsonIgnore
    public void setAuthorities(Set<String>  permissionList) {
        this.authorities = mapToGrantedAuthorities(permissionList);
    }

    private static List<GrantedAuthority> mapToGrantedAuthorities(List<String>  permissionList) {
        Set<String> permissions = new HashSet<>();
        permissions.addAll(permissionList);
        return permissions.stream()
                .map(permission -> new SimpleGrantedAuthority(permission))
                .collect(Collectors.toList());
    }

    private static List<GrantedAuthority> mapToGrantedAuthorities(Set<String>  permissionList) {
        Set<String> permissions = new HashSet<>();
        permissions.addAll(permissionList);
        return permissions.stream()
                .map(permission -> new SimpleGrantedAuthority(permission))
                .collect(Collectors.toList());
    }
}
